/*
 * ============================================================================
 *
 *  Zombie:Reloaded
 *
 *  File:          zcookies.inc
 *  Type:          Module
 *  Description:   ZCookies module, allows clients to modify their ZR cookies via menu.
 *
 *  Copyright (C) 2009-2010  Greyscale, Richard Helgeby
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ============================================================================
 */

/**
 * Create commands specific to ZHP.
 */
ZCookiesOnCommandsCreate()
{
    // Register ZCookies command.
    RegConsoleCmd(SAYHOOKS_KEYWORD_ZCOOKIES, ZCookiesCommand, "Toggle all ZR cookies here.");
}

/**
 * Show ZCookies menu to client.
 * 
 * @param client    The client index.
 */
ZCookiesMenuMain(client)
{
    // Create menu handle.
    new Handle:zcookies_menu_main = CreateMenu(ZCookiesMenuMainHandle);
    
    // Make client global translations target.
    SetGlobalTransTarget(client);
    
    decl String:autorebuyenabled[MENU_LINE_SMALL_LENGTH];
    decl String:zhpenabled[MENU_LINE_SMALL_LENGTH];
    decl String:overlayenabled[MENU_LINE_SMALL_LENGTH];
    
    // Get the current toggle state of the cookies.
    ConfigBoolToSetting(CookiesGetBool(client, g_hZMarketAutoRebuyCookie), autorebuyenabled, sizeof(autorebuyenabled), false, client);
    ConfigBoolToSetting(CookiesGetBool(client, g_hZHPEnabledCookie), zhpenabled, sizeof(zhpenabled), false, client);
    ConfigBoolToSetting(CookiesGetBool(client, g_hOverlayEnabledCookie), overlayenabled, sizeof(overlayenabled), false, client);
    
    decl String:title[MENU_LINE_TITLE_LENGTH];
    decl String:autorebuy[MENU_LINE_REG_LENGTH];
    decl String:zhp[MENU_LINE_REG_LENGTH];
    decl String:overlay[MENU_LINE_REG_LENGTH];
    decl String:zmarket[MENU_LINE_REG_LENGTH];
    
    // Translate each line into client's language.
    Format(title, sizeof(title), "%t\n ", "ZCookies Menu main title");
    Format(autorebuy, sizeof(autorebuy), "%t", "ZCookies menu main auto-rebuy", autorebuyenabled);
    Format(zhp, sizeof(zhp), "%t", "ZCookies menu main zhp", zhpenabled);
    Format(overlay, sizeof(overlay), "%t", "ZCookies menu main overlay", overlayenabled);
    Format(zmarket, sizeof(zmarket), "%t", "ZCookies zmarket weapon setup");
    
    // Get conditional values for each option.
    new bool:weapons = (GetConVarBool(g_hCvarsList[CVAR_WEAPONS]) && ConfigIsConfigLoaded(File_Weapons));   // For auto-rebuy.
    new bool:zmarketrebuyauto = GetConVarBool(g_hCvarsList[CVAR_ZMARKET_REBUY_AUTO]);                       // For auto-rebuy.
    new bool:zhpcvar = GetConVarBool(g_hCvarsList[CVAR_ZHP]);                                               // For ZHP.
    new bool:overlaytoggle = GetConVarBool(g_hCvarsList[CVAR_CLASSES_OVERLAY_TOGGLE]);                      // For class overlay.
    new bool:zmarketenabled = GetConVarBool(g_hCvarsList[CVAR_ZMARKET]);                                    // For ZMarket weapon setup.
    
    // Add items to menu.
    SetMenuTitle(zcookies_menu_main, title);
    AddMenuItem(zcookies_menu_main, "autorebuy", autorebuy, MenuGetItemDraw(weapons && zmarketrebuyauto));
    AddMenuItem(zcookies_menu_main, "zhp", zhp, MenuGetItemDraw(zhpcvar));
    AddMenuItem(zcookies_menu_main, "overlay", overlay, MenuGetItemDraw(overlaytoggle));
    AddMenuItem(zcookies_menu_main, "zmarket", zmarket, MenuGetItemDraw(zmarketenabled));
    
    // Create a "Back" button to the main menu.
    SetMenuExitBackButton(zcookies_menu_main, true);
    
    // Display menu to client.
    DisplayMenu(zcookies_menu_main, client, MENU_TIME_FOREVER);
}

/**
 * Menu callback (main)
 * Toggles client cookies.
 * 
 * @param menu      The menu handle.
 * @param action    Action client is doing in menu.
 * @param client    The client index.
 * @param slot      The menu slot selected. (starting from 0)
 */
public ZCookiesMenuMainHandle(Handle:menu, MenuAction:action, client, slot)
{
    // Client selected an option.
    if (action == MenuAction_Select)
    {
        new bool:resend = true;
        
        switch(slot)
        {
            // Toggled auto-rebuy
            case 0:
            {
                // Toggle the auto-rebuy cookie.
                ZMarketToggleAutoRebuy(client);
            }
            // Toggled ZHP.
            case 1:
            {
                ZHPToggle(client);
            }
            // Toggled class overlay.
            case 2:
            {
                // If overlay toggle is disabled, then stop.
                new bool:overlaytoggle = GetConVarBool(g_hCvarsList[CVAR_CLASSES_OVERLAY_TOGGLE]);
                if (overlaytoggle)
                {
                    // Toggle current overlay channel, retrieve new value, and update cookie.
                    new bool:overlayenabled = OverlaysClientSetChannelState(client, OVERLAYS_CHANNEL_CLASSES, true, true);
                    CookiesSetBool(client, g_hOverlayEnabledCookie, overlayenabled);
                }
            }
            // View ZMarket weapon setup.
            case 3:
            {
                // Show a client their current weapon setup.
                ZMarketMenuWeaponSetup(client);
                
                // Don't resend ZCookies.
                resend = false;
            }
        }
        
        if (resend)
        {
            // Re-send menu.
            ZCookiesMenuMain(client);
        }
    }
    // Client closed the menu.
    if (action == MenuAction_Cancel)
    {
        // Client hit "Back" button.
        if (slot == MenuCancel_ExitBack)
        {
            // Re-open main menu.
            ZMenuMain(client);
        }
    }
    // Client exited menu.
    if (action == MenuAction_End)
    {
        CloseHandle(menu);
    }
}

/**
 * Command callback (zcookies)
 * Toggle all ZR cookies here.
 * 
 * @param client    The client index.
 * @param argc      Argument count.
 */
public Action:ZCookiesCommand(client, argc)
{
    // If client is console, then stop and tell them this feature is for players only.
    if (ZRIsConsole(client))
    {
        TranslationPrintToServer("Must be player");
        return Plugin_Handled;
    }
    
    // Send ZCookies menu.
    ZCookiesMenuMain(client);
    
    // This stops the "Unknown command" message in client's console.
    return Plugin_Handled;
}